package com.ljw.easyrulestest1;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.InferenceRulesEngine;
import org.jeasy.rules.core.RuleBuilder;

/**
 * @author lijunwei
 * @date 2024/2/4
 *
 * Easy Rules 提供了两种RulesEngine接口实现：
 *
 * DefaultRulesEngine：根据其自然顺序应用规则（默认为优先级）。
 * InferenceRulesEngine：不断地对已知事实应用规则，直到不再适用规则为止。
 *
 * DefaultRulesEngine的作用很好理解，就像上面那些例子表现出来的一样。
 * InferenceRulesEngine则相当于在DefaultRulesEngine的基础上加了一个循环。还是以开头的代码举例，换成InferenceRulesEngine。
 * 这时控制台将重复打印“It rains, take an umbrella!”。
 *
 * 作者：bailiyi
 * 链接：https://www.jianshu.com/p/3bc5773a1545
 * 来源：简书
 * 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
 */
public class InferenceRulesEngineTest {


    public static void main(String[] args) {
        // define rules
        Rule weatherRule = new RuleBuilder()
                .name("weather rule")
                .description("if it rains then take an umbrella")
                .when(facts -> facts.get("rain").equals(true))
                .then(facts -> System.out.println("It rains, take an umbrella!"))
                .build();

        Rules rules = new Rules();
        rules.register(weatherRule);

        // define facts
        Facts facts = new Facts();
        facts.put("rain", true);

        // fire rules on known facts
        RulesEngine rulesEngine = new InferenceRulesEngine();
        rulesEngine.fire(rules, facts);
    }

}
